/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.rest.backend.utils;

import cz.muni.fi.pa165.creatures.dto.WeaponDTO;
import java.io.IOException;
import java.io.InputStream;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * This class builds a {@link cz.muni.fi.pa165.creatures.dto.WeaponDTO}.
 *
 * @author smikloso
 */
public class WeaponDTOBuilder implements DTOBuilder<WeaponDTO> {

    @Override
    public WeaponDTO build(InputStream weaponData)
            throws SAXException, IOException, ParserConfigurationException, WebApplicationException {

        WeaponDTO dto = new WeaponDTO();

        DocumentBuilder documentBuilder =
                DocumentBuilderFactory.newInstance().newDocumentBuilder();

        Document document = (Document) documentBuilder.parse(weaponData);
        document.getDocumentElement().normalize();

        NodeList nodeList = document.getElementsByTagName("weapon");

        Node creatureRoot = nodeList.item(0);

        if (creatureRoot == null) {
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        if (creatureRoot.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) creatureRoot;

            NodeList childNodes = element.getChildNodes();

            for (int i = 0; i < childNodes.getLength(); i++) {
                Element childElement = (Element) childNodes.item(i);
                String tagName = childElement.getTagName();
                String textContent = childElement.getTextContent();

                if (tagName.equals("id")) {
                    dto.setId(textContent);
                } else if (tagName.equals("name")) {
                    dto.setName(textContent);
                } else if (tagName.equals("range")) {
                    dto.setRange(new Integer(textContent));
                } else if (tagName.equals("ammunition")) {
                    dto.setAmmunition(new Integer(textContent));
                }
            }
        } else {
            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
        }
        return dto;
    }
}
